package com.example.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCExample {
    public static void main(String[] args)  throws Exception{
        //1. 注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2. 获取连接
        String url = "jdbc:mysql://localhost:3306/javaweb";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        //3. 定义sql
        String sql = "SELECT * FROM tb_brand";
        //4. 获取执行sql的对象 Statement
        Statement statement = conn.createStatement();
        //5. 执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        //6. 处理结果
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String brand_name = resultSet.getString("brand_name");
            String company_name = resultSet.getString("company_name");
            String description = resultSet.getString("description");
            System.out.println("id："+id+" ,brand_name:"+brand_name+" ,company_name:"+company_name+ " ,description:"+description);
        }
        //7. 释放资源
        statement.close();
        conn.close();
    }
}
